/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EnderecoPanel.java
 *
 * Created on 20/10/2011, 23:07:28
 */
package br.com.mocamorena.view.panel;

import br.com.mocamorena.control.ControleEndereco;
import br.com.mocamorena.exception.PersistenciaException;
import br.com.mocamorena.model.Endereco;
import br.com.mocamorena.view.StateEntityWindow;
import br.com.mocamorena.view.dialog.ConsultaEnderecoDialog;
import java.util.List;
import javax.swing.JLabel;

/**
 *
 * @author Diego
 */
public class EnderecoPanel extends javax.swing.JPanel {

    private Endereco endereco = null;

    public EnderecoPanel() {
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cepField = new javax.swing.JFormattedTextField();
        logradouroField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        complementoField = new javax.swing.JTextField();
        bairroField = new javax.swing.JTextField();
        cidadeField = new javax.swing.JTextField();
        logradouroErrorLabel = new javax.swing.JLabel();
        abrirEnderecosButton = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        estadoField = new javax.swing.JTextField();

        jLabel1.setText("CEP:");

        jLabel3.setText("Endereço:");

        try {
            cepField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####-###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        cepField.setToolTipText("Digite o cep e tecle ENTER para localizar o endereço");
        cepField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);
        cepField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cepFieldActionPerformed(evt);
            }
        });

        logradouroField.setEditable(false);
        logradouroField.setFocusable(false);

        jLabel2.setText("Complemento:");

        jLabel4.setText("Bairro:");

        jLabel5.setText("Cidade:");

        bairroField.setEditable(false);
        bairroField.setFocusable(false);

        cidadeField.setEditable(false);
        cidadeField.setFocusable(false);

        logradouroErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/exclamation.png"))); // NOI18N

        abrirEnderecosButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/magnifier.png"))); // NOI18N
        abrirEnderecosButton.setToolTipText("Abrir consulta de endereços");
        abrirEnderecosButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        abrirEnderecosButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirEnderecosButtonActionPerformed(evt);
            }
        });

        jLabel6.setText("Estado:");

        estadoField.setEditable(false);
        estadoField.setFocusable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cepField, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(abrirEnderecosButton, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(complementoField)
                            .addComponent(logradouroField, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(logradouroErrorLabel))
                    .addComponent(estadoField, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(cidadeField, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(bairroField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cepField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(abrirEnderecosButton, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(logradouroErrorLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(logradouroField)
                        .addComponent(jLabel3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(complementoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(bairroField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cidadeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(estadoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {abrirEnderecosButton, cepField});

    }// </editor-fold>//GEN-END:initComponents

    private void abrirEnderecosButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirEnderecosButtonActionPerformed
        ConsultaEnderecoDialog dialog = new ConsultaEnderecoDialog(new javax.swing.JFrame(), true, StateEntityWindow.SEARCH);
        dialog.setSelectEntity(true);
        dialog.setVisible(true);
        Endereco endereco = dialog.getEndereco();
        setEndereco(endereco);
    }//GEN-LAST:event_abrirEnderecosButtonActionPerformed

    private void cepFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cepFieldActionPerformed
        String cep = cepField.getText().replace("-", "");
        try {
            ControleEndereco controle = new ControleEndereco();
            List<Endereco> lista = controle.listarPeloCep(cep);
            setEndereco(lista.isEmpty() ? null : lista.get(0));
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }//GEN-LAST:event_cepFieldActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton abrirEnderecosButton;
    private javax.swing.JTextField bairroField;
    private javax.swing.JFormattedTextField cepField;
    private javax.swing.JTextField cidadeField;
    private javax.swing.JTextField complementoField;
    private javax.swing.JTextField estadoField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel logradouroErrorLabel;
    private javax.swing.JTextField logradouroField;
    // End of variables declaration//GEN-END:variables

    public Endereco getEndereco() {
        if (endereco != null) {
            endereco.setComplemento(complementoField.getText().trim().toUpperCase());
        }
        return endereco;
    }

    public void setEndereco(Endereco endereco) {
        if (endereco != null) {
            this.endereco = endereco;
            cepField.setText(endereco.getCep());
            logradouroField.setText(endereco.getLogradouro());
            bairroField.setText(endereco.getBairro());
            cidadeField.setText(endereco.getCidade());
            estadoField.setText(endereco.getUf().name());
            complementoField.setText(endereco.getComplemento());
        }
    }

    public JLabel getLogradouroErrorLabel() {
        return logradouroErrorLabel;
    }

    public String getLogradouro() {
        return logradouroField.getText();
    }
}
